$(function ()  {
    window.ExpressNo=0;//全局面单数量
    getLetterSumAndMoneySum();
    $(".submit-file").click(function(){
        upload();
        return false;
    });
    $(".btn-addRow").click(function () {
        addRow();
    });
    $(".btn-orderSub").click(function () {
        if($(".table-add-order").find("input").hasClass("input-isNull")){
            window.wxc.xcConfirm("请完善订单信息", window.wxc.xcConfirm.typeEnum.warning);
        }else {
            submitOrders();
        }
    });
})
function upload() {
    var formData = new FormData($('.importBusinessForm')[0]);
    $.ajax({
        url: '/JRD/order_import.json',
        type: 'POST',
        success: function(data){
            importOrder(data.list);
        },
        error: function(data){
            window.wxc.xcConfirm(data.msg, window.wxc.xcConfirm.typeEnum.warning);
        },
        data: formData,
        cache: false,
        contentType: false,
        processData: false
    });
}
function importOrder(arr) {
  $.each(arr,function (n,value) {
      $(".table-add-order").append("<tr>"+
          "<td><input type='checkbox' id='isQuick"+n+"' ></td>" +
          "<td><input type='checkbox' id='isLocalCity"+n+"'></td>" +
          "<td><button class='del btn btn-primary btn-sm btn-table'>删除</button></td>" +
          "<td><input value='"+(value.orderNo==null?"":value.orderNo)+"' class='orderNo form-control' maxlength='6'></td>" +
          "<td><input value='"+(value.itemName==null?"":value.itemName)+"' class='item-name form-control'></td>" +
          "<td><input value='"+(value.toName==null?"":value.toName)+"' class='toName form-control'></td>" +
          "<td><input value='"+(value.cellphone==null?"":value.cellphone)+"' minlength='11' class='cellphone form-control'></td>" +
          "<td><input value='"+(value.city==null?"":value.city)+"' class='city form-control'></td>" +
          "<td><input value='"+(value.area==null?"":value.area)+"' class='area form-control'></td>" +
          "<td><input value='"+(value.street==null?"":value.street)+"' class='street form-control'></td>" +
          "<td><input value='"+(value.address==null?"":value.address)+"' class='address form-control'></td>" +
          "<td><input value='"+(value.weight==null?"":value.weight)+"' class='weight form-control'></td>" +
          "<td><input value='"+(value.price==null?"":value.price)+"' class='price form-control'></td>" +
          "</tr>");
      $(".del").unbind("click");
      $(".del").click(function () {
          $(this).parent().parent().remove();
      });
      if(value.isQuick==1){
          $("#isQuick"+n)[0].checked=true;
      }
      if(value.isLocalCity==1){
          $("#isLocalCity"+n)[0].checked=true;
      }
  })
}
function getLetterSumAndMoneySum() {
    $.getJSON("/JRD/order_check.json",function (msg) {
        if(msg.success==true){
            window.ExpressNo=msg.ExpressNo;
        }else{
            window.ExpressNo=0;
        }
    })
}
function addRow() {
    var counter=0;
    $(".table-add-order tr:gt(0)").each(function (n,value) {
            counter++;
    });
    $(".table-add-order").append("<tr>"+
        "<td><input type='checkbox' id='isQuick"+counter+"'></td>" +
        "<td><input type='checkbox' id='isLocalCity"+counter+"'></td>" +
        "<td><button class='del btn btn-primary btn-sm btn-table'>删除</button></td>" +
        "<td><input value='' class='orderNo form-control'></td>" +
        "<td><input value='' class='itemName form-control'></td>" +
        "<td><input value='' class='toName form-control'></td>" +
        "<td><input value='' class='cellphone form-control'></td>" +
        "<td><input value='' class='address form-control'></td>" +
        "<td><input value='' class='weight form-control'></td>" +
        "<td><input value='' class='price form-control'></td>" +
        "</tr>");
    $(".del").unbind("click");
    $(".del").click(function () {
        $(this).parent().parent().remove();
    });
}
function submitOrders() {
        var orderArr=[];
        var order ={};
        var counter=0;
        var user = {};//录入者
        var date;//录入时间
        var orderNo;//订单编号
        var itemName;//产品名称
        var price;//价格
        var weight;//物体重量
        var toName;//收件人
        var cellphone;//电话号码
        var city;//城市
        var area;//区县
        var street;//街道
        var address;//详细地址
        var isChoose;//是否勾选协议
        var isQuick;//是否加急
        var isLocalCity;//是否同城
        $(".table-add-order tr:gt(0)").each(function (n,value) {
            if($(this).find("input").hasClass("input-isNull")){
                $(this).find("input").removeClass("input-isNull");
            }
            counter++;
            order = {};
            isQuick = $("#isQuick"+n)[0].checked;
            isLocalCity = $("#isLocalCity"+n)[0].checked;
            orderNo = $(this).find(".orderNo").val();
            itemName = $(this).find(".item-name").val();
            toName = $(this).find(".toName").val();
            cellphone = $(this).find(".cellphone").val();
            city = $(this).find(".city").val();
            area = $(this).find(".area").val();
            street = $(this).find(".street").val();
            address = $(this).find(".address").val();
            price = $(this).find(".price").val();
            weight= $(this).find(".weight").val();
            if(orderNo==""){
                $(this).find(".orderNo").addClass("input-isNull");
            }else if(orderNo.length<6){
                $(this).find(".orderNo").addClass("input-isNull");
            }else if(itemName==""){
                $(this).find(".itemName").addClass("input-isNull");
            }else if(toName==""){
                $(this).find(".toName").addClass("input-isNull");
            }else if(cellphone==""){
                $(this).find(".cellphone").addClass("input-isNull");
            }else if(cellphone.length<11){
                $(this).find(".cellphone").addClass("input-isNull");
            } else if(city==""){
                $(this).find(".city").addClass("input-isNull");
            }else if(area==""){
                $(this).find(".area").addClass("input-isNull");
            }else if(street==""){
                $(this).find(".street").addClass("input-isNull");
            }else if(weight==""){
                $(this).find(".weight").parent().addClass("input-isNull");
            }else if(price==""){
                $(this).find(".price").addClass("input-isNull");
            }else {
                order.orderNo = orderNo;
                order.itemName=itemName;
                order.toName=toName;
                order.cellphone=cellphone;
                order.city=city;
                order.area=area;
                order.street=street;
                order.address=address;
                order.price=price;
                order.weight=weight;
                order.isQuick=isQuick;
                order.isLocalCity=isLocalCity;
                orderArr.push(order);
            }
        });
        if(orderArr.length>0&&orderArr.length==counter){
            if(orderArr.length>window.ExpressNo){
                window.wxc.xcConfirm("剩余面单数量不足", window.wxc.xcConfirm.typeEnum.warning);
            }else {
                $.ajax({
                    url:"/JRD/order_input_vip.json",
                    type:"POST",
                    data:JSON.stringify(orderArr),
                    contentType:"application/json; charset=utf-8",
                    dataType:"json",
                    success: function(msg){
                        if(msg.success==true){
                            $(".table-add-order tr:gt(0)").remove();
                            getexrpessNoandBalance();
                            window.wxc.xcConfirm(msg.msg, window.wxc.xcConfirm.typeEnum.success);
                        }else {
                            window.wxc.xcConfirm(msg.msg, window.wxc.xcConfirm.typeEnum.error);
                        }
                    }
                })
            }
        }
}